package cn.bonoon.kernel.menus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.bonoon.kernel.config.SortableItem;
import cn.bonoon.kernel.security.ChannelPrivilege;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.util.StringHelper;

public final class ModuleInfo extends SortableItem implements IModuleInfo{

	private int menuWidth;
	private int topHeight;
	private int footHeight;
	private String systemLogo;
	private String footName;
	private String systemTitle;
	private String systemName;
	private DesInfo des;
	List<PartInfo> parts;

	/**
	 * 子系统的缩略图
	 */
	private String thumbnail;
	private String endpoint;
	private String path;

	private final List<ChannelInfo> channels;

	/**
	 * 该功能下面的所有菜单的集合
	 */
	private final Map<String, ItemInfo> itemCached;
	
	public ModuleInfo(List<ChannelInfo> channels) {
		this.channels = channels;
		this.itemCached = new ConcurrentHashMap<String, ItemInfo>();
	}
	
	//这里的originalId没有被加密过
	ModuleInfo visit(String originalId, IModuleContext context) {
		if(StringHelper.isNotEmpty(systemName)){
			systemName = "<img src='" + context.path(systemName) + "' alt='" + name + "'/>";
		}else{
			systemName = name;
		}
		
		for (ChannelInfo channel : channels) {
			channel.init(context, this, itemCached, originalId, path);
		}
		// 排序
		Collections.sort(channels);
		return this;
	}
	
	public ItemInfo getItem(String id) {
		return itemCached.get(id);
	}
	
	@Override
	public IStyleInfo getStyle(){
		return new StyleProxy(this);
	}
	
	@Override
	public DesInfo getDes() {
		return des;
	}

	@Override
	public String getSystemName() {
		return systemName;
	}
	
	@Override
	public final List<ChannelInfo> getChannels(IOperator user, ChannelPrivilege privilege) {
		//进行预处理
		List<ChannelInfo> cis = new ArrayList<ChannelInfo>();
		for (ChannelInfo channel : channels) {
			if (privilege.hasPrivilege(channel)) {
				// 有权限访问这个频道，但不一定有权限访问该频道所指向的子菜单
				// 如果频道不是直接使用url，而是指定子菜单，并没有该子菜单的访问权限
				// 则需要重新从有权限的子菜单下取出第一个有权限的子菜单
				cis.add(channel);
			}
		}
		return cis;
	}
	
	public final List<ChannelInfo> getChannels() {
		return channels;
	}

	public final String getThumbnail() {
		return thumbnail;
	}

	public final void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}

	public final String getEndpoint() {
		return endpoint;
	}

	public final void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public int getMenuWidth() {
		return menuWidth;
	}

	public void setMenuWidth(int menuWidth) {
		this.menuWidth = menuWidth;
	}

	public int getTopHeight() {
		return topHeight;
	}

	public void setTopHeight(int topHeight) {
		this.topHeight = topHeight;
	}

	public String getSystemLogo() {
		return systemLogo;
	}

	public void setSystemLogo(String systemLogo) {
		this.systemLogo = systemLogo;
	}

	public int getFootHeight() {
		return footHeight;
	}

	public void setFootHeight(int footHeight) {
		this.footHeight = footHeight;
	}

	public String getFootName() {
		return footName;
	}

	public void setFootName(String footName) {
		this.footName = footName;
	}

	public String getSystemTitle() {
		return systemTitle;
	}

	public void setSystemTitle(String systemTitle) {
		this.systemTitle = systemTitle;
	}

	public void setDes(DesInfo des) {
		this.des = des;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public List<PartInfo> getParts() {
		return parts;
	}

	public void setParts(List<PartInfo> parts) {
		this.parts = parts;
	}

	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}
}
